% (1,lambda)-sigma SA-ES Slide 92
% Michael Sieber, Philipp Rusch

lambda = 10;

% initialization (line 1)
N = 10;
yp = randn(N, 1);
sigma_p = 1;
sigma_stop = 0.0002;

% initialize generation counter (line 2)
g = 0;

% generation and fitness log for plotting
plot_generations(g+1) = [g];
plot_fitness(g+1) = [Kugelmodell(yp)];

% evolution loop (line 3)
while(sigma_p > sigma_stop)
	for l=1:lambda
		yl.sigma(l) = LogNormal(N) * sigma_p;
		yl.offspring(:,l) = yp + yl.sigma(l) * randn(N, 1);
		yl.fitness(l) = Kugelmodell(yl.offspring(:,l));
	endfor
	
	% get the sigma and offspring with the best fitness
	[bestfit, idx] = min(yl.fitness);
	sigma_p = yl.sigma(idx);
	yp = yl.offspring(:, idx);
	
	% generation increase (line 11)
	g = g+1;
	
	% add the plot values
	plot_generations(g) = [g];
	plot_fitness(g) = [bestfit];
endwhile

% print the result
clf();
semilogy(plot_generations, plot_fitness);
ylabel("Parental Fitness F(y)");
xlabel("Generations");
axis([0, 200, 10^-14, 10]); %[xmin, xmax, ymin, ymax]